package 笔试.bilibili笔试.第二题括号;

import java.util.Stack;
//括号的合法性
public class Solution
{
    /**
     *
     * @param s string字符串
     * @return bool布尔型
     */
    public boolean IsValidExp (String s)
    {
        if(s == null)
            return true;

        Stack<Character> stack = new Stack<>();
        for(char c : s.toCharArray())
        {
            if(c=='(')
                stack.push(')');
            else if(c=='[')
                stack.push(']');
            else if(c=='{')
                stack.push('}');
            else
            {
                if(stack.isEmpty() || stack.pop() != c)
                    return false;
            }
        }
        return stack.isEmpty();
    }

}

